var e = require;
var t = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var n = e("BaseModuleUI"),
    a = e("GlobalParams"),
    i = e("AssetsManager"),
    r = e("PureHelper"),
    s = e("Game"),
    l = e("node_team"),
    c = e("role_effects_info"),
    d = e("role_node"),
    h = e("role_skill"),
    _ = cc._decorator,
    u = _.ccclass,
    p =
        (_.property,
        (function (e) {
            function t() {
                var t = (null !== e && e.apply(this, arguments)) || this;
                return (
                    (t.role_node = null),
                    (t.role_solider = null),
                    (t.team_info = s.TeamInfo.rest_team),
                    (t.ai_disabled = !1),
                    (t.ai_addHp = !1),
                    (t.home_pos = null),
                    (t.ai_attack = !1),
                    (t.add_heal = null),
                    (t.ack_range = 0),
                    (t.is_map_traget = !1),
                    (t.traget_pos = cc.v2(0, 0)),
                    t
                );
            }
            return (
                __extends(t, e),
                (t.prototype.initView = function () {
                    (this.is_map_traget = !1),
                        (this.home_pos = this.node.getPosition()),
                        (this.role_node = this.node.getComponent(d.default)),
                        (this.role_solider = this.node.parent),
                        (this.team_info = this.node.getComponent(l.default).getTeamInfo()),
                        this.updateTime();
                }),
                (t.prototype.start = function () {
                    this.ack_range = this.node.getComponent(h.default).ack_range;
                }),
                (t.prototype.updateTime = function () {
                    var e = this;
                    if (a.GlobalParams.GameStart)
                        if (this.is_map_traget) {
                            if (r.default.Distance(this.node.getPosition(), this.traget_pos) < 50)
                                this.is_map_traget = !1;
                            else {
                                var t = cc
                                    .v2(this.traget_pos.x, this.traget_pos.y)
                                    .sub(this.node.getPosition())
                                    .normalize();
                                this.role_node.roleMoveInfo(t);
                            }
                        } else
                            this.role_node.role_death ||
                                (this.ai_disabled
                                    ? this.node.y > -430 && this.node.y < 110
                                        ? this.goHomeAddHp()
                                        : this.getAttackTarget()
                                    : this.role_node.getRoleHpPercent() < 25 && !a.GlobalParams.PlayGuideMode
                                    ? (this.ai_disabled = !0)
                                    : ((this.ai_addHp = !1), this.getAttackTarget()));
                    this.scheduleOnce(function () {
                        e.updateTime();
                    }, a.GlobalParams.GameUpdateTime);
                }),
                (t.prototype.getAttackTarget = function () {
                    for (var e = 9999, t = null, o = 0, n = this.role_solider.children; o < n.length; o++) {
                        var a = n[o],
                            i = a.getComponent(l.default).getTeamInfo();
                        if (cc.isValid(a, !0) && i != this.team_info && i != s.TeamInfo.rest_team) {
                            var c = Math.abs(r.default.Distance(this.node.getPosition(), a.getPosition()));
                            c < e && ((e = c), (t = a));
                        }
                    }
                    if (
                        (this.ai_attack || (this.node.scaleX = this.node.x > t.x ? 1 : -1),
                        (this.ai_attack = !1),
                        e > 2e3)
                    )
                        this.role_node.roleMoveInfo(cc.v2(-1 * this.node.scaleX, 0));
                    else if (e > this.ack_range) {
                        var d = cc.v2(t.x, t.y).sub(this.node.getPosition()).normalize();
                        this.role_node.roleMoveInfo(d);
                    } else
                        Math.abs(t.y - this.node.y) > 50
                            ? this.role_node.roleMoveInfo(cc.v2(0, t.y > this.node.y ? 1 : -1))
                            : (this.role_node.roleStopMove(),
                              (this.ai_attack = !0),
                              "role" == t.group ? this.role_node.AISpecialSkill() : this.role_node.roleCommonSkill());
                }),
                (t.prototype.goHomeAddHp = function () {
                    var e = Math.abs(r.default.Distance(this.node.getPosition(), this.home_pos));
                    if (this.ai_addHp)
                        this.role_node.setRoleAddHp(),
                            this.role_node.getRoleHpPercent() > 75 &&
                                ((this.ai_disabled = !1), this.add_heal.destroy());
                    else if (e <= 50) {
                        this.role_node.roleStopMove();
                        var t = cc.instantiate(i.default.getInstance().getFightPrefab("role_effects"));
                        (this.add_heal = t),
                            t.setParent(this.node),
                            t.getComponent(c.default).showEffectInfo(s.RoleEffectType.home_heal),
                            (this.ai_addHp = !0);
                    } else {
                        var o = cc.v2(this.home_pos.x, this.home_pos.y).sub(this.node.getPosition()).normalize();
                        this.role_node.roleMoveInfo(o),
                            this.role_node.getRoleHpPercent() > 75 && (this.ai_disabled = !1);
                    }
                }),
                (t.prototype.onCollisionEnter = function (e, t) {
                    var o = e.node;
                    if ("map" == o.group && 0 == t.tag && !this.is_map_traget) {
                        this.is_map_traget = !0;
                        var n = o.children[e.tag].getPosition();
                        (n = o.convertToWorldSpaceAR(n)),
                            (n = this.node.parent.convertToNodeSpaceAR(n)),
                            (this.traget_pos = n);
                    }
                }),
                __decorate([u], t)
            );
        })(n.default));
o.default = p;
